package engine.newUi
{
	import engine.control.NxTablePageTurner;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.flag.ResourcesPath;
	import engine.logic.LogSendLogic;
	import engine.logic.MenuListManager;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.tip.tipItem.PetTipItem;
	import engine.ui.AlertUI;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import newx.com.config.StaticIni;
	import newx.com.control.tab.NxTab;
	import newx.com.control.tab.NxTabEvent;
	import newx.com.control.table.NxTableEvent;
	import newx.com.control.window.NxWindowEvent;
	
	/**
	 * 排行榜
	 * @author yangyuechao
	 */
	public class RankLogic
	{
		private static var _self:RankLogic
		
		private var _myDis:RankUI;
		private var _model:ModelLocator;
		private var _bindManage:BindManage;
		
		private var _isBind:Boolean				=	false;
		
		//管理翻页
		private var _pageTurner		:NxTablePageTurner;
		//保存排行榜的数组
		private var _rankVector:Vector.<RankVo>;
		//保存宠物排行榜数组
		private var _petRankVector:Vector.<PetRankVo>;
		
		private var _level:int;
		private var _name:String;
		
		private var _levelRank:int = 0;
		private var _attRank:int = 0;
		private var _petRank:int = 0;
		
		private var _currentTabIndex:int = 0;
		
		private var _petTips:PetTipItem;
		
		public function RankLogic()
		{
			_bindManage			=	BindManage.getInstance();
			_model				=	ModelLocator.getInstance();
			_myDis				=	RankUI.getInstance();
			_petTips			=	new PetTipItem();
			_pageTurner	= new NxTablePageTurner(_myDis.upBtn,_myDis.nextBtn,_myDis.rankTable,_myDis.pageLabel);
		}
		public static function getInstance():RankLogic
		{
			if(_self == null)
			{
				_self = new RankLogic();
			}
			return _self;
		}
		
		public function BeforeShow():void
		{
			_rankVector			= new Vector.<RankVo>();
			_petRankVector		= new Vector.<PetRankVo>();
			
			_currentTabIndex = 0;
			_myDis.rankTable.dataProvider = null;
			
			_myDis.rankTab.clearItems();
			_myDis.rankTab.addItem(StaticIni.getSwfLangStr("UIString_1000657"));
			_myDis.rankTab.addItem(StaticIni.getSwfLangStr("UIString_1000658"));
			_myDis.rankTab.addItem(StaticIni.getSwfLangStr("UIString_1000659"));
			_myDis.rankTab.selected = 0;
			facePic;
			//请求等级排行 
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_PLAYER_TOPLIST);
			//请求战力排行
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_FIGHT_POWER_TOPLIST);
			//请求宠物战力排行
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_PET_TOP);
			
			addListener();
		}
		
		/**
		 * 开始监听
		 */		
		public function addListener():void
		{
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_PLAYER_TOPLIST), getRankList);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_FIGHT_POWER_TOPLIST), getRankList);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_PET_TOP), getRankList);
			
			_pageTurner.addEventListener();
			_myDis.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE, closeWindowHandle);
			
			_myDis.rankTab.addEventListener(MouseEvent.CLICK, changeTab);
			
			_myDis.rankTable.addEventListener(NxTableEvent.TABLE_CLICK,	tableClickHandle);
			if(!_isBind)
			{
				_isBind	=	true;
				_bindManage.BindProp(_model.scene.Role,"Level", this, "level");
				_bindManage.BindProp(_model.scene.Role,"Name", this, "name");
			}
		}
		/**
		 * 移除监听
		 */		
		public function removeListener():void
		{
			_pageTurner.removeEventListener();
			_model.removeEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_PLAYER_TOPLIST), getRankList);
			_model.removeEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_FIGHT_POWER_TOPLIST), getRankList);
			_myDis.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE, closeWindowHandle);
			_myDis.rankTab.removeEventListener(MouseEvent.CLICK, changeTab);
			_myDis.rankTable.removeEventListener(NxTableEvent.TABLE_CLICK,	tableClickHandle);
		}
		
		/**
		 * 关闭窗口 
		 */		
		private function closeWindowHandle(e:NxWindowEvent):void
		{
			_myDis.hide();
		}
		
		/**
		 * 玩家等级 
		 */		
		public function set level(val:int):void
		{
			if(val != _level){
				_level = val;
//				_myDis.proLevel.textField.autoSize	=	TextFieldAutoSize.CENTER;
				_myDis.proLevel.htmlText	=	"Lv <font color = '#33CC00'>" +  _level + "</font>";
			}
		}
		
		/**
		 * 玩家名字
		 */
		public function set name(val:String):void
		{
			if(val != _name){
				_name = val;
//				_myDis.proName.textField.autoSize = TextFieldAutoSize.CENTER;
				switch(val.length){
					case 1:
						_myDis.proName.x = 97;
						break;
					case 2:
						_myDis.proName.x = 91;
						break;
					case 3:
						_myDis.proName.x = 86;
						break;
					case 4:
						_myDis.proName.x = 80;
						break;
					case 5:
						_myDis.proName.x = 74;
						break;
					default:
						_myDis.proName.x = 68;
						break;
				}
				_myDis.proName.htmlText		=	_name;
			}
		}
		
		/**
		 * 玩家头像资源路径
		 */
		public function get facePic():String
		{
			var url:String=ResourcesPath.getHeadPic(_model.scene.Role.ModelId, _model.scene.Role.clotheModel);
			_myDis.proPic.source		=	url;
			return url;
		}
		
		/**
		 * 接受到排行列表
		 */
		private function getRankList(e:CustomMsgEvent):void
		{
			if(!_myDis.created) return;
			var serverArr:Array = e.pList;
			if(serverArr.length == 0) return;
			
			if (e.type == String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_PLAYER_TOPLIST)){
				_levelRank = serverArr[0];
			} else if(e.type == String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_FIGHT_POWER_TOPLIST) ){
				_attRank = serverArr[0];
				_myDis.proFightPower.htmlText	= "<font color = '#33CC00'>" + serverArr[5] + "</font>";
			} else if(e.type == String(CustomToClientFlag.SERVER_CUSTOMMSG_PET_TOP) ){
				_petRank = serverArr[0] + 1;
			}
			
			if(_levelRank > 30 || _levelRank <= 0){
				_myDis.proLevRank.htmlText	=	"<font color = '#33CC00'>" + StaticIni.getSwfLangStr("UIString_1000661") + "</font>";
			} else {
				_myDis.proLevRank.htmlText	=	"<font color = '#33CC00'>" + String(_levelRank) + "</font>";
			}
			if(_attRank > 30 || _attRank <= 0){
				_myDis.proAttRank.htmlText	=	"<font color = '#33CC00'>" + StaticIni.getSwfLangStr("UIString_1000661") + "</font>";
			} else {
				_myDis.proAttRank.htmlText	=	"<font color = '#33CC00'>" + String(_attRank) + "</font>";
			}
			if(_petRank > 30 || _petRank <= 0){
				_myDis.proPetRank.htmlText	=	"<font color = '#33CC00'>" + StaticIni.getSwfLangStr("UIString_1000661") + "</font>";
			} else {
				_myDis.proPetRank.htmlText	=	"<font color = '#33CC00'>" + String(_petRank) + "</font>";
			}
			
			if(_currentTabIndex == 0 && e.type == String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_PLAYER_TOPLIST)){
				saveRank(serverArr);
			} else if(_currentTabIndex == 1 && e.type == String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_FIGHT_POWER_TOPLIST)){
				saveRank(serverArr);
			} else if(_currentTabIndex == 2 && e.type == String(CustomToClientFlag.SERVER_CUSTOMMSG_PET_TOP)){
				savePetRank(serverArr);
			}
		}
		
		private function saveRank(arr:Array):void
		{
			var rank:int = 1;
			_rankVector		= new Vector.<RankVo>();
			for(var i:int=6;i<arr.length;i+=5)
			{
				if(rank > 30) break;
				//名字,等级,门派,帮派,战斗力,排名
				var vo :RankVo=new RankVo(arr[i],arr[i+1],arr[i+2],arr[i+3],arr[i+4],rank);
				if(_rankVector.indexOf(vo.name) == -1)
				{
					_rankVector.push(vo);
				}
				rank++;
			}
			
			showList(1);
		}
		
		private function savePetRank(arr:Array):void
		{
			var rank:int = 1;
			_petRankVector		= new Vector.<PetRankVo>();
			for(var i:int=1;i<arr.length;i+=6)
			{
				if(rank > 30) break;
				//拥有者，名字,等级,品阶,属性等级,战斗力,排名
				var vo :PetRankVo=new PetRankVo(arr[i],arr[i+1],arr[i+2],arr[i+3],arr[i+4],arr[i+5],rank);
				if(_petRankVector.indexOf(vo.owner) == -1)
				{
					_petRankVector.push(vo);
				}
				rank++;
			}
			
			showList(1);
		}
		
		/**
		 * 表格点击
		 */		
		private function tableClickHandle(e:NxTableEvent):void
		{
			if(e.lineArrData == null) return;
			
			if(_currentTabIndex != 2){
				var str:String	=	e.lineArrData[1].label;
				if(str == "") return;
				var textField:TextField = new TextField();
				textField.htmlText = str;
				var playName:String = textField.text;
				if(playName != _model.scene.Role.getPlayerName())
				{
					MenuListManager.clickPlayer(playName);
				}
			}
		}
		
		private function tableOverHandler(e:NxTableEvent):void
		{
			TipsMainLogic.instance.hide();
			var currentIndex:int;
			if(_myDis.pageLabel.text.substr(0,1) == "1"){
				currentIndex = e.pos.row;
			} else if(_myDis.pageLabel.text.substr(0,1) == "2"){
				currentIndex = e.pos.row + 10;
			} else if(_myDis.pageLabel.text.substr(0,1) == "3"){
				currentIndex = e.pos.row + 20;
			}
			if(currentIndex >= e.target.dataProvider.length) return;
			var obj:Object = e.target.dataProvider[currentIndex][1]["data"];
			if(obj == null) return;
			if(_currentTabIndex == 2){
				var _dataArr:Array = new Array();
				_dataArr.push(0, obj.name, obj.grade, obj.level, obj.proLevel, obj.fightPower);
				TipsMainLogic.instance.showString(_petTips.getPetTips(_dataArr));
			}
		}
		
		private function tableOutHandler(e:NxTableEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		/**
		 * 页签
		 */
		private function changeTab(e:MouseEvent):void
		{
			var nxTab:NxTab = e.currentTarget as NxTab;
			if(nxTab.selected == _currentTabIndex) return;
			_currentTabIndex = nxTab.selected;
			
			//选择等级排行
			if(_currentTabIndex == 0){
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_PLAYER_TOPLIST);
			//选择战力排行
			}else if(_currentTabIndex == 1){
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_FIGHT_POWER_TOPLIST);
			//选择宠物排行
			}else if(_currentTabIndex == 2){
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_PET_TOP);
				_myDis.rankTable.addEventListener(NxTableEvent.TABLE_ROLL_OVER, tableOverHandler);
				_myDis.rankTable.addEventListener(NxTableEvent.TABLE_ROLL_OUT, tableOutHandler);
			}
		}
		
		/**
		 * 填充列表
		 */
		private function showList(val:int):void
		{
			_myDis.rankTable.dataProvider = null;
			
			var itemArr:Array = [];
			//等级排行
			if(_currentTabIndex == 0){
				_myDis.rankTable.titleLabels = [ StaticIni.getSwfLangStr("Common_Sort"), StaticIni.getSwfLangStr("Common_Name"), StaticIni.getSwfLangStr("Common_Level") ];
				for(var i:int = 0;i<_rankVector.length;i++){
					if(_rankVector[i].rank == 1){
						itemArr.push( [{label:("<font color = '#FFFF00'>" + _rankVector[i].rank + "</font>")}, 
							{label:("<font color = '#FFFF00'>" + _rankVector[i].name + "</font>")},
							{label:("<font color = '#FFFF00'>" + _rankVector[i].level + "</font>")}]);
					} else if(_rankVector[i].rank == 2){
						itemArr.push( [{label:("<font color = '#BA55D3'>" + _rankVector[i].rank + "</font>")}, 
							{label:("<font color = '#BA55D3'>" + _rankVector[i].name + "</font>")},
							{label:("<font color = '#BA55D3'>" + _rankVector[i].level + "</font>")}]);
					} else if(_rankVector[i].rank == 3){
						itemArr.push( [{label:("<font color = '#00BFFF'>" + _rankVector[i].rank + "</font>")}, 
							{label:("<font color = '#00BFFF'>" + _rankVector[i].name + "</font>")},
							{label:("<font color = '#00BFFF'>" + _rankVector[i].level + "</font>")}]);
					} else {
						itemArr.push( [{label:(_rankVector[i].rank)}, 
							{label:(_rankVector[i].name)},
							{label:(_rankVector[i].level)}]);
					}
					
				}
			//战力排行
			} else if(_currentTabIndex == 1){
				_myDis.rankTable.titleLabels = [ StaticIni.getSwfLangStr("Common_Sort"), StaticIni.getSwfLangStr("Common_Name"), StaticIni.getSwfLangStr("RankingUI_Str_5") ];
				for(i = 0;i<_rankVector.length;i++){
					if(_rankVector[i].rank == 1){
						itemArr.push( [{label:("<font color = '#FFFF00'>" + _rankVector[i].rank + "</font>")}, 
							{label:("<font color = '#FFFF00'>" + _rankVector[i].name + "</font>")},
							{label:("<font color = '#FFFF00'>" + _rankVector[i].fightPower + "</font>")}]);
					} else if(_rankVector[i].rank == 2){
						itemArr.push( [{label:("<font color = '#BA55D3'>" + _rankVector[i].rank + "</font>")}, 
							{label:("<font color = '#BA55D3'>" + _rankVector[i].name + "</font>")},
							{label:("<font color = '#BA55D3'>" + _rankVector[i].fightPower + "</font>")}]);
					} else if(_rankVector[i].rank == 3){
						itemArr.push( [{label:("<font color = '#00BFFF'>" + _rankVector[i].rank + "</font>")}, 
							{label:("<font color = '#00BFFF'>" + _rankVector[i].name + "</font>")},
							{label:("<font color = '#00BFFF'>" + _rankVector[i].fightPower + "</font>")}]);
					} else {
						itemArr.push( [{label:(_rankVector[i].rank)}, 
							{label:(_rankVector[i].name)},
							{label:(_rankVector[i].fightPower)}]);
					}
					
				}
			//宠物战力排行
			} else if(_currentTabIndex == 2){
				_myDis.rankTable.titleLabels = [ StaticIni.getSwfLangStr("Common_Sort"), StaticIni.getSwfLangStr("UIString_1000660"), StaticIni.getSwfLangStr("RankingUI_Str_5") ];
				for(i = 0;i<_petRankVector.length;i++){
					if(_petRankVector[i].rank == 1){
						itemArr.push( [{label:("<font color = '#FFFF00'>" + _petRankVector[i].rank + "</font>")}, 
							{label:("<font color = '#FFFF00'>" + _petRankVector[i].owner + "-" + _petRankVector[i].name + "</font>"), data:_petRankVector[i]},
							{label:("<font color = '#FFFF00'>" + _petRankVector[i].fightPower + "</font>")}]);
					} else if(_petRankVector[i].rank == 2){
						itemArr.push( [{label:("<font color = '#BA55D3'>" + _petRankVector[i].rank + "</font>")}, 
							{label:("<font color = '#BA55D3'>" +_petRankVector[i].owner + "-" + _petRankVector[i].name + "</font>"), data:_petRankVector[i]},
							{label:("<font color = '#BA55D3'>" + _petRankVector[i].fightPower + "</font>")}]);
					} else if(_petRankVector[i].rank == 3){
						itemArr.push( [{label:("<font color = '#00BFFF'>" + _petRankVector[i].rank + "</font>")}, 
							{label:("<font color = '#00BFFF'>" + _petRankVector[i].owner + "-" + _petRankVector[i].name + "</font>"), data:_petRankVector[i]},
							{label:("<font color = '#00BFFF'>" + _petRankVector[i].fightPower + "</font>")}]);
					} else {
						itemArr.push( [{label:(_petRankVector[i].rank)}, 
							{label:(_petRankVector[i].owner + "-" + _petRankVector[i].name), data:_petRankVector[i]},
							{label:(_petRankVector[i].fightPower)}]);
					}
				}
			}
			
			_myDis.rankTable.dataProvider = itemArr;
			_pageTurner.onPageDis();
		}
	}
}
/**
 * 排行榜
 */
class RankVo
{
	public var name		:String; 	 //名字
	public var level		:int;		//等级
	public var prof		:int;		//门派
	public var faction		:String		//帮派
	public var fightPower	:int;		//战斗力
	public var rank		:int;		//排名
	public function RankVo(name:String, level:int, prof:int, faction:String, fightPower:int, rank:int):void
	{
		this.name		= name;
		this.level		= level;
		this.prof		= prof;
		this.faction	= faction;
		this.fightPower	= fightPower;
		this.rank		= rank;
	}
}
/**
 * 宠物战力排行榜
 */
class PetRankVo
{
	public var owner		:String;	//拥有者
	public var name		:String; 	//名字
	public var level		:int;		//等级
	public var grade		:int;		//品阶
	public var proLevel	:int		//属性等级
	public var fightPower	:int;		//战斗力
	public var rank		:int;		//排名
	
	public function PetRankVo(owner:String, name:String, level:int , grade:int, proLevel:int, fightPower:int, rank:int):void
	{
		this.owner		= owner;
		this.name		= name;
		this.level		= level;
		this.grade		= grade;
		this.proLevel	= proLevel;
		this.fightPower	= fightPower;
		this.rank		= rank;
	}
}